﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace PIM_ATE.Electrical
{
    [Serializable]
    public class clsDBBase
    {
        public int iColumnNum;
        public string[] m_arrDbHead, m_arrExcelHead;

        public clsDBBase()
        {
        }

        public string GetExcelStr()
        {
            StringBuilder sbExcel = new StringBuilder(256);
            for (int i = 0; i < m_arrExcelHead.Length; i++)
            {
                sbExcel.Append("[");
                sbExcel.Append(m_arrExcelHead[i]);
                sbExcel.Append("]");
                if (i != m_arrExcelHead.Length - 1)
                {
                    sbExcel.Append(", ");
                }
            }
            return sbExcel.ToString();
        }

        public string GetDbStr(int iStart)
        {
            if (iStart >= m_arrDbHead.Length)
            {
                return "";
            }
            StringBuilder sbDb = new StringBuilder(256);

            for (int i = iStart; i < m_arrDbHead.Length; i++)
            {
                sbDb.Append(m_arrDbHead[i]);
                if (i != m_arrDbHead.Length - 1)
                {
                    sbDb.Append(", ");
                }
            }
            return sbDb.ToString();
        }

        public string GetDbStr(int iStart, int iAsWhat, string strAsWhat)
        {
            if (iStart >= m_arrDbHead.Length)
            {
                return "";
            }
            StringBuilder sbDb = new StringBuilder(256);

            for (int i = iStart; i < m_arrDbHead.Length; i++)
            {
                if (i == iAsWhat)
                    sbDb.Append(strAsWhat);
                else
                    sbDb.Append(m_arrDbHead[i]);
                if (i != m_arrDbHead.Length - 1)
                {
                    sbDb.Append(", ");
                }
            }
            return sbDb.ToString();
        }

        public string GetxxxDbStr(string xType, int iStart)
        {
            if (iStart >= m_arrDbHead.Length)
            {
                return "";
            }
            StringBuilder sbDb = new StringBuilder(256);

            for (int i = iStart; i < m_arrDbHead.Length; i++)
            {
                sbDb.Append(xType);
                sbDb.Append(m_arrDbHead[i]);
                if (i != m_arrDbHead.Length - 1)
                {
                    sbDb.Append(", ");
                }
            }
            return sbDb.ToString();
        }

        public string GetDbToExcelStr(int iStart)
        {
            if (iStart >= m_arrDbHead.Length)
            {
                return "";
            }
            StringBuilder sbDb = new StringBuilder(256);
            for (int i = iStart; i < m_arrDbHead.Length; i++)
            {
                sbDb.Append(m_arrDbHead[i]);
                sbDb.Append(" as ");
                sbDb.Append("[");
                sbDb.Append(m_arrExcelHead[i]);
                sbDb.Append("]");
                if (i != m_arrDbHead.Length - 1)
                {
                    sbDb.Append(", ");
                }
            }
            return sbDb.ToString();
        }

        public string GetxxxDbToExcelStr(string xType, int iStart)
        {
            if (iStart >= m_arrDbHead.Length)
            {
                return "";
            }
            StringBuilder sbDb = new StringBuilder(256);
            for (int i = iStart; i < m_arrDbHead.Length; i++)
            {
                sbDb.Append(xType);
                sbDb.Append(m_arrDbHead[i]);
                sbDb.Append(" as ");
                sbDb.Append("[");
                sbDb.Append(m_arrExcelHead[i]);
                sbDb.Append("]");
                if (i != m_arrDbHead.Length - 1)
                {
                    sbDb.Append(", ");
                }
            }
            return sbDb.ToString();
        }
    }

    public class clsProject : clsDBBase
    {
        public string ProjectId { get; set; }
        public string projectName { get; set; }
        //测试模式，0:旧的单个指标mark取点 , 1:新的测试模式，采用fdata批量测试模式,2:在新的测试模式下，可以不断保存测试结果
        public string sNewTestMode { get; set; }
        //校准频段
        public string Band { get; set; }
        //条码长度
        public int iCodeLen { get; set; }

        public static string chn_ProjectID = "方案编号";
        public static string chn_ProjectName = "方案名";
        public static string chn_TestMode = "测试模式";
        public static string chn_Band = "校准带宽";

        public clsProject()
        {
            iColumnNum = 11;
            m_arrDbHead = new string[iColumnNum];
            int i = 0;
            m_arrDbHead[i++] = "[ProjectID]";
            m_arrDbHead[i++] = "[ProjectName]";
            m_arrDbHead[i++] = "[NewTestMode]";
            m_arrDbHead[i++] = "[Band]";

            m_arrExcelHead = new string[iColumnNum];
            i = 0;
            m_arrExcelHead[i++] = chn_ProjectID;
            m_arrExcelHead[i++] = chn_ProjectName;
            m_arrExcelHead[i++] = chn_TestMode;
            m_arrExcelHead[i++] = chn_Band;
        }

    }

    [Serializable]
    public class clsTestItem : clsDBBase
    {// 测试项

        public string[] m_arrRCUHead;

        #region trace 
        public int iTestCount;
        public float f1, f2;
        public int power;
        public int iTestIndex;
        public bool result;
        public string sValue;
        #endregion

        public string sTestItem = ""; // 测试项
        public string sTestName = ""; // 参数名称
        public string sSpec = ""; // "客户要求"
        public string sFactSpec = ""; // 内控
        public string sInstrCtrl = ""; // 仪表控制列
        public string sFreq = ""; // 频率
        public string sRandom = ""; //测试次数 随机范围//1,2, 50~80
        public string sTestFlag = ""; //true X
        public string sPortA = ""; // 1端口连接
        public string sPortB = ""; // 2端口连接
        public string sTestPortType = ""; // 测试类型//S11,S21
        public string sFormatType = ""; // 格式化类型//DISP_LOGM
        public string sReport = ""; // 报表内容
        public string sIndex = ""; // 序号
        public string sBand = ""; // 校准频段

        public string sPortA_Angle = "";
        public string sPortB_Angle = "";
        public string sPortA_RCU = "";
        public string sPortB_RCU = "";
        public string sTestFunction = "";
        public string sRCUFlag = "1";

        public bool sCheckResult = true;
        public int sRetSpeed = 2;
        public int sPoint = 201;
        public string sFileVer = "1.0";
        public static string chn_Point = "Point";
        public static string chn_FileVer = "工艺文件版本";

        /**********************端口提示*************/
        public string sPortA_Tip = "";
        public string sPortB_Tip = "";
        public static string chn_PortA_Tip = "1端口提示";
        public static string chn_PortB_Tip = "2端口提示";
        /**********************端口提示*************/

        /********************FAD、混频天线******************/
        public string sMarkers;//marker
        public int sAntennaType;//天线类型 普通0/FAD 1/ 混频2
        public int sTestMethod;//测试方法  一次性0/分次2
        public int sTestType;//测试类型   普通0/带间1/福相2
        public static string chn_sMarkers = "标记点";
        public static string chn_sAntennaType = "天线类型";
        public static string chn_sTestMethod = "是否分测";
        public static string chn_sTestType = "测试类型";
        /********************FAD、混频天线******************/

        public static string chn_ItemName = "测试项";
        public static string chn_ParamName = "参数名称";
        public static string chn_FactSpec = "内控要求";
        public static string chn_Freq = "测试频率";
        public static string chn_Band = "校准频段";
        public static string chn_TestNo = "TestNo";
        public static string chn_Done = "Done";
        public static string chn_PortA = "1端口连接";
        public static string chn_PortB = "2端口连接";
        public static string chn_PortType = "测试内容";
        public static string chn_FormatType = "测试数据2";
        public static string chn_Report = "报表内容";
        public static string chn_InstrCtrl = "仪表控制列";
        public static string chn_Index = "TestIndex";

        public static string chn_PortA_Angle = "1端口角度";
        public static string chn_PortB_Angle = "2端口角度";
        public static string chn_PortA_RCU = "1端口RCU";
        public static string chn_PortB_RCU = "2端口RCU";
        public static string chn_TestFunction = "测试方法";
        public static string chn_RCUFlag = "测试标识";

        public static string chn_Value = "测试值";
        public static string chn_Result = "判定结果";
        public static string chn_LastValue = "上次值";
        public static string chn_LastResult = "上次结果";
        public static string chn_NowValue = "实时测试值";

        

        public clsTestItem()
        {
            iColumnNum = 22;
            int i = 0;
            m_arrExcelHead = new string[iColumnNum];
            m_arrExcelHead[i++] = "测试项";
            m_arrExcelHead[i++] = chn_ParamName;
            m_arrExcelHead[i++] = "客户要求";
            m_arrExcelHead[i++] = chn_FactSpec;
            m_arrExcelHead[i++] = chn_Freq;
            m_arrExcelHead[i++] = chn_Band;
            m_arrExcelHead[i++] = chn_TestNo;//1,2, 50~80
            m_arrExcelHead[i++] = chn_Done;//true X
            m_arrExcelHead[i++] = chn_PortA;
            m_arrExcelHead[i++] = chn_PortB;
            m_arrExcelHead[i++] = chn_PortType;
            m_arrExcelHead[i++] = chn_FormatType;
            m_arrExcelHead[i++] = chn_Report;
            m_arrExcelHead[i++] = chn_InstrCtrl;
            m_arrExcelHead[i++] = "非导出项";
            m_arrExcelHead[i++] = chn_Index;

            m_arrExcelHead[i++] = chn_sMarkers;
            m_arrExcelHead[i++] = chn_sAntennaType;
            m_arrExcelHead[i++] = chn_sTestMethod;
            m_arrExcelHead[i++] = chn_sTestType;

            m_arrExcelHead[i++] = chn_Point;
            m_arrExcelHead[i++] = chn_FileVer;

            i = 0;
            m_arrDbHead = new string[iColumnNum];
            m_arrDbHead[i++] = "[UnitName]";
            m_arrDbHead[i++] = "[ParamName]";
            m_arrDbHead[i++] = "[Spec]";
            m_arrDbHead[i++] = "[FactSpec]";
            m_arrDbHead[i++] = "[Freq]";
            m_arrDbHead[i++] = "[Band]";
            m_arrDbHead[i++] = "[SwitchLoad]";//true X
            m_arrDbHead[i++] = "[TestFlag]";//1,2, 50~80
            m_arrDbHead[i++] = "[PortA]";
            m_arrDbHead[i++] = "[PortB]";
            m_arrDbHead[i++] = "[PortType]";
            m_arrDbHead[i++] = "[FormatType]";
            m_arrDbHead[i++] = "[Report]";
            m_arrDbHead[i++] = "[InstrCtrl]";
            m_arrDbHead[i++] = "[unreport]";
            m_arrDbHead[i++] = "[TestIndex]";

            m_arrDbHead[i++] = "[Markers]";
            m_arrDbHead[i++] = "[AntennaType]";
            m_arrDbHead[i++] = "[TestMethod]";
            m_arrDbHead[i++] = "[TestType]";

            m_arrDbHead[i++] = "[Point]";
            m_arrDbHead[i++] = "[FileVer]";


            i = 0;
            m_arrRCUHead = new string[8];
            m_arrRCUHead[i++] = chn_PortA_Angle;
            m_arrRCUHead[i++] = chn_PortB_Angle;
            m_arrRCUHead[i++] = chn_PortA_RCU;
            m_arrRCUHead[i++] = chn_PortB_RCU;
            m_arrRCUHead[i++] = chn_PortA_Tip;
            m_arrRCUHead[i++] = chn_PortB_Tip;

            m_arrRCUHead[i++] = chn_TestFunction;
            m_arrRCUHead[i++] = chn_RCUFlag;
        }
    }

    class ClsDataSParamBuilder : ClsDataConfigBase
    {
        DataTable m_dtTestItem;

        public ClsDataSParamBuilder(string alias)
            : base(alias)
        {
            CONFIG_TYPE = "SPARAM";
            CONDITION_SQL = " and [Type] = '" + CONFIG_TYPE + "';";
        }

        #region static function

        public static DataTable toTable(List<clsTestItem> lstConfig)
        {
            clsTestItem cTestItem = new clsTestItem();
            DataTable dt = new DataTable();
            dt.Columns.Add("序号ID");
            for (int i = 0; i < cTestItem.m_arrExcelHead.Length; i++)
            {
                dt.Columns.Add(cTestItem.m_arrExcelHead[i]);
            }

            for (int i = 0; i < cTestItem.m_arrRCUHead.Length; i++)
            {
                dt.Columns.Add(cTestItem.m_arrRCUHead[i]);
            }

            for (int n = 0; n < lstConfig.Count; n++)
            {
                int i = 0;
                DataRow dr = dt.NewRow();
                dr[i++] = n.ToString();

                dr[i++] = lstConfig[n].sTestItem;
                dr[i++] = lstConfig[n].sTestName;

                dr[i++] = lstConfig[n].sSpec;
                dr[i++] = lstConfig[n].sFactSpec;
                dr[i++] = lstConfig[n].sFreq;
                dr[i++] = lstConfig[n].sBand;
                dr[i++] = lstConfig[n].sRandom;
                dr[i++] = lstConfig[n].sTestFlag;
                dr[i++] = lstConfig[n].sPortA;
                dr[i++] = lstConfig[n].sPortB;

                dr[i++] = lstConfig[n].sTestPortType;
                dr[i++] = lstConfig[n].sFormatType;
                dr[i++] = lstConfig[n].sReport;
                dr[i++] = lstConfig[n].sInstrCtrl;
                dr[i++] = "0";//非导出项
                dr[i++] = lstConfig[n].sIndex;

                dr[i++] = lstConfig[n].sMarkers;
                dr[i++] = lstConfig[n].sAntennaType;
                dr[i++] = lstConfig[n].sTestMethod;
                dr[i++] = lstConfig[n].sTestType;

                dr[i++] = lstConfig[n].sPoint;
                dr[i++] = lstConfig[n].sFileVer;

                dr[i++] = lstConfig[n].sPortA_Angle;
                dr[i++] = lstConfig[n].sPortB_Angle;
                dr[i++] = lstConfig[n].sPortA_RCU;
                dr[i++] = lstConfig[n].sPortB_RCU;

                dr[i++] = lstConfig[n].sPortA_Tip;
                dr[i++] = lstConfig[n].sPortB_Tip;

                dr[i++] = lstConfig[n].sTestFunction;
                dr[i++] = lstConfig[n].sRCUFlag;                

                dt.Rows.Add(dr);
            }
            return dt;
        }

        #region serialize

        public static clsTestItem[] DeserializeFile(string xmlFilePath)
        {
            try
            {
                clsTestItem[] SerializerObject;
                var xmlSerializer = new XmlSerializer(typeof(clsTestItem[]));
                using (var sr = new StreamReader(xmlFilePath))
                {
                    SerializerObject = (clsTestItem[])xmlSerializer.Deserialize(sr);
                }

                return SerializerObject;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return null;
            }
        }

        public static clsTestItem[] DeserializeString(string origin)
        {
            try
            {
                clsTestItem[] SerializerObject;
                var xmlSerializer = new XmlSerializer(typeof(clsTestItem[]));
                using (TextReader reader = new StringReader(origin))
                {
                    SerializerObject = (clsTestItem[])xmlSerializer.Deserialize(reader);
                }

                return SerializerObject;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return null;
            }
        }

        public static string SerializeString(clsTestItem[] serializerObject)
        {
            try
            {
                var xmlSerializer = new XmlSerializer(typeof(clsTestItem[]));
                StringBuilder sb = new StringBuilder();
                XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true });
                xmlSerializer.Serialize(writer, serializerObject);
                writer.Close();

                return XmlUtil.FormatXml(sb.ToString());
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return "";
            }
        }

        public static bool SerializeFile(string sPath, clsTestItem[] serializerObject)
        {
            try
            {
                var xmlSerializer = new XmlSerializer(typeof(clsTestItem[]));
                StringBuilder sb = new StringBuilder();
                StreamWriter writer = new StreamWriter(sPath);
                xmlSerializer.Serialize(writer, serializerObject);
                writer.Close();

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
        }

        #endregion

        #endregion

        public bool upload(string sProcode, string sVersion, List<clsTestItem> SerializerObject, ref string error_info)
        {//上传更新配置
            string strXml = SerializeString(SerializerObject.ToArray());
            if (strXml == "")
            {
                return false;
            }

            return uploadXml(sProcode, sVersion, strXml, ref error_info);
        }

        public DataTable download(out List<clsTestItem> lstConfig)
        {
            string sXml = downloadXml();
            lstConfig = null;
            if (sXml != null || sXml != "")
            {
                lstConfig = new List<clsTestItem>();
                lstConfig.AddRange(DeserializeString(sXml));
                if (lstConfig.Count > 0)
                {
                    m_dtTestItem = toTable(lstConfig);
                    return m_dtTestItem;
                }
            }
            return null;
        }

        public DataTable download(string sXmlFilePath, out List<clsTestItem> lstConfig)
        {
            m_dtTestItem = null;
            lstConfig = null;
            string xmlFilePath = sXmlFilePath;
            var serialObj = DeserializeFile(xmlFilePath);
            if (serialObj == null)
            {
                return null;
            }

            lstConfig = new List<clsTestItem>();
            lstConfig.AddRange(serialObj);

            return toTable(lstConfig);
        }

        public override DataTable getProCodeTable()
        {
            List<clsTestItem> lstConfig;
            return download(out lstConfig);
        }

    }
}
